package com.zzdn.mqtool.service.impl;

import com.zzdn.mqtool.entity.MQConfig;
import com.zzdn.mqtool.service.MQService;
import com.zzdn.mqtool.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Service
public class MQServiceImpl implements MQService {
    private static Logger logger = LoggerFactory.getLogger(MQServiceImpl.class);

    @Autowired
    private MQConfig config;

    @Autowired
    private LocalFileReadUtils reader;

    @Override
    public Map<String, Object> doSendXml(String xmlContent,int priority,String messageId,String correlationId, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        try {
            String ip = "MQ-" + IPTool.getClientIp(request);
            logger.info("访问客户端IP: "+ ip);
            MQConfig cfg = (MQConfig) LocalCacheClient.get(ip);
            if (cfg == null){
                cfg = config;
            }
            logger.info(cfg.toString());

            MQManager manager = new MQManager(cfg);
            boolean b = manager.sendMsg(config.getRemoteQueueName(),messageId, correlationId, xmlContent,priority);
            if (b){
               logger.info("报文发送成功,messageID : {}, correlationId : {}, priority : {} " , messageId, correlationId, priority);
               logger.info("报文发送成功: {} " , xmlContent);
            }
            manager.disconnect();

            ResultMap.setSuccess(map);
        }catch(Exception e){
            logger.error(e.getMessage());
            ResultMap.setfail(map,"Maybe you should configure the MQ parameter ! ! ");
        }finally {
        }
        return map;
    }

    @Override
    public Map<String, Object> saveMqConfig(MQConfig cfg,HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        try {
            String ip = "MQ-" + IPTool.getClientIp(request);

            logger.info("当前序列 ： {} ",ip);
            LocalCacheClient.set(ip, cfg);

            ResultMap.setSuccess(map);
        }catch(Exception e){
            ResultMap.setfail(map);
        }
        return map;
    }

    @Override
    public Map<String,Object> getXmlTemplate(String tradeNo){
        Map<String, Object> map = new HashMap<>();
        try{
            String xml = reader.readXml(tradeNo);
            ResultMap.setStatusAndData(map , "0000", xml);
        }catch (Exception e){
            logger.error(e.getMessage());
            ResultMap.setStatusAndData(map,"0001","读取模板失败！！");
        }
        return map;
    };

    @Override
    public Map<String,Object> getDefault(){
        Map<String, Object> map = new HashMap<>();
        return ResultMap.setStatusAndData(map,"0000",  config);
    };

    @Override
    public Map<String,Object> getUserConfig(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        try{
            String ip = "MQ-" + IPTool.getClientIp(request);
            MQConfig cfg = (MQConfig) LocalCacheClient.get(ip);
            if(cfg != null){
                ResultMap.setStatusAndData(map,"0000",  cfg);
            }
            else {
                ResultMap.setfail(map, "You need to reconfigure the MQ parameter !!! ");
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            ResultMap.setfail(map);
        }

        return map;
    };



}
